<?php
namespace app\admin\controller;

use app\common\model\Orders;
use app\common\model\User;
use app\common\model\UserAccount;


/**
 * 管理员控制器
 * Class Admin
 * @package app\admin\controller
 */
class Report extends Base
{
    public $begin;
    public $end;
    public function initialize(){
        parent::initialize();
        $start_time = $this->request->get('start_time');
        if($start_time){
            $begin = urldecode($start_time);
            $end_time = $this->request->get('end_time');
            $end = urldecode($end_time);
        }else{
            $begin = date('Y-m-d', strtotime("-3 month"));//30天前
            $end = date('Y-m-d');
        }
        $this->assign('start_time',$begin);
        $this->assign('end_time',$end);
        $this->begin = strtotime($begin);
        $this->end = strtotime($end)+86399;
    }

    /**
     * 销售状况统计报表
     * @return mixed
     */
    public function index(){
        $query = Orders::where('ord_status',1)->select()->toArray();
        $now = strtotime(date('Y-m-d'));
        $today = Orders::where('ord_status',1)->
        where("ord_create_time",'>',$now)->field("count(ord_total_price)/100 today_amount,count(ord_id) today_num")->find();//今日销售总额
        if($today['today_amount'] == 0){
            $today['sign'] = round(0,2);
        } else {
            $today['sign'] = round($today['today_amount'] / $today['today_num'], 2);
        }
        $this->assign('today',$today);

        $begin = $this->begin;
        $end = $this->end;

        $res = Orders::where('ord_status',1)->where("ord_create_time",'>=',$begin)->where("ord_create_time","<=",$end)
            ->field("count(ord_total_price)/100 today_amount,count(ord_id) today_num,FROM_UNIXTIME(ord_create_time,'%Y-%m-%d') as gap")
            ->group('gap')->select()->toArray();
//        dump($today);die;
        $tnum = 0;
        $tamount=0;
        foreach ($res as $val){
            $arr[$val['gap']] = $val['today_num'];
            $brr[$val['gap']] = $val['today_amount'];
            $tnum += $val['today_num'];
            $tamount += $val['today_amount'];
        }

        for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
            $tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
            $tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : round($brr[date('Y-m-d',$i)],2);
            $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
            $order_arr[] = $tmp_num;
            $amount_arr[] = $tmp_amount;
            $sign_arr[] = $tmp_sign;
            $date = date("Y-m-d",$i);
            $list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
            $day[] = $date;
        }
        rsort($list);
        $this->assign('list',$list);
        $result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
//        dump($result);die;
//        var_dump($result);die;
        $this->assign('sign',json_encode($sign_arr));
        $this->assign('order',json_encode($order_arr));
        $this->assign('amount',json_encode($amount_arr));
        $this->assign('time',json_encode($day,true));
        return $this->fetch();
    }

    /**
     * 会员统计
     * @return mixed
     */
    public function user(){
        $today = strtotime(date('Y-m-d'));
        $month = strtotime(date('Y-m-01'));
        $user['today'] = User::where("create_time > $today")->count();//今日新增会员
        $user['month'] = User::where("create_time > $month")->count();//本月新增会员
        $user['total'] = User::count();//会员总数
        $user['user_account'] = UserAccount::sum('ua_account');//会员余额总额
        $user['user_coupons'] = UserAccount::sum('ua_coupons');//会员红包余额总额
        $res = Orders::cache(true)->distinct(true)->field('ord_user_id')->select();
        $user['hasorder'] = count($res);
        $this->assign('user',$user);
        $new = User::where("FROM_UNIXTIME(create_time,'%Y-%m-%d')",'>=',$this->begin)
            ->where("FROM_UNIXTIME(create_time,'%Y-%m-%d')","<=",$this->end)
            ->field("count(*) as num,FROM_UNIXTIME(create_time,'%Y-%m-%d') as gap")
            ->group('gap')->select()->toArray();//新增会员趋势

        foreach ($new as $val){
            $arr[$val['gap']] = $val['num'];
        }

        for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
            $brr[] = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
            $day[] = date('Y-m-d',$i);
        }
        $result = array('data'=>$brr,'time'=>$day);
        $this->assign('result',$result);
        return $this->fetch();
    }
}